﻿using System;
using System.Net;
using System.Collections.Generic;
using SCG.General;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //CloakDetector ShipType
    public class CloakDetector : StarShip
    {
        //constructor
        public CloakDetector(SVGame game, Player owner, int engines, int detectorRange, StarSystem origin)
            : base(game, owner, engines, detectorRange, origin)
        {
        }

        //ship type name
        public override string Name
        {
            get
            {
                return "CloakDetector";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "CloakDetectors detect cloaked StarShips that are within their Detector Range.  They pulse when cloaked StarShips are within range, and display arrows indicating the direction(s) of the cloaked ships.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.CloakDetector;
            }
        }

        //The list of visible cloak arrows
        public List<int> CloakArrows
        {
            get
            {
                return _cloakArrows;
            }
        }

        //Is the CloakDetector pulsing? (at least 1 cloak arrow visible)
        public bool IsPulsing
        {
            get
            {
                return CloakArrows.Count > 0;
            }
        }

        //private members
        private List<int> _cloakArrows = new List<int>();

        //detect cloaked enemies and notify client
        protected internal override void CommonPostProcessing()
        {
            CloakArrows.Clear();
            foreach (StarShip cloaked in Game.StarShips)
                if (DistanceQuick(cloaked) <= Value * 2)
                    if (!cloaked.VisibleTo[Owner] && cloaked.Cloaked && cloaked.Owner != Owner)
                        if (cloaked.Distance(this) <= Value)
                            if (Game.GetTerrain(X, Y) == ' ')
                            {
                                //Get the direction relative to the ship
                                int direction = Game.DirectionTowardPoint(Location, cloaked.Location);
                                if (!CloakArrows.Contains(direction) && direction != -1)
                                    CloakArrows.Add(direction);
                            }

            //Notify client
            if (CloakArrows.Count > 0 && OwnedByCurrentPlayer)
                if (Game.Host != null)
                    Game.Host.CloakedShipsDetected(this);
        }

        //render cloak detector range
        public override SCGColor RadiusColor
        {
            get
            {
                return CloakDetectorColor;
            }
        }

        //private members
        private static SCGColor CloakDetectorColor = new SCGColor(128, 128, 128, 128);
    }
}
